{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e323d8e3-fe87-4c27-ba1a-56d88cf7ddf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice's average score: 89.00, Max score: 92, Min score: 85\n",
      "Class 1A scores analysis:\n",
      "Course 0 - Average score: 89.00, Max score: 90, Min score student: 88, Min score: 1\n",
      "Course 1 - Average score: 90.00, Max score: 95, Min score student: 85, Min score: 0\n",
      "Course 2 - Average score: 87.00, Max score: 92, Min score student: 82, Min score: 1\n",
      "Alice's scores: [90, 85, 92]\n",
      "Class 1A students: {'S001': 'Alice', 'S002': 'Bob'}\n",
      "Class 1A students' scores for course 0: {'S001': 90, 'S002': 88}\n",
      "Class 1A students after removing Alice: {'S002': 'Bob'}\n"
     ]
    }
   ],
   "source": [
    "class GradeAnalyzer:\n",
    "    def __init__(self):\n",
    "        self.classes = {}  # 存储班级信息\n",
    "\n",
    "    def add_class(self, class_id):\n",
    "        if class_id not in self.classes:\n",
    "            self.classes[class_id] = {}  # 如果班级ID不存在，则初始化该班级的学生字典\n",
    "\n",
    "    def add_student(self, class_id, student_id, name, scores):\n",
    "        # 在指定班级中添加一个学生\n",
    "        if class_id in self.classes:\n",
    "            # 如果班级存在，则添加学生信息\n",
    "            self.classes[class_id][student_id] = {'name': name, 'scores': scores}\n",
    "        else:\n",
    "            # 如果班级不存在，则提示错误信息\n",
    "            print(f\"Class {class_id} does not exist. Add the class first.\")\n",
    "\n",
    "    def analyze_student_scores(self, class_id, student_id):\n",
    "        # 分析指定学生的成绩\n",
    "        if class_id in self.classes and student_id in self.classes[class_id]:\n",
    "            # 如果班级和学生都存在，则计算平均分、最高分和最低分\n",
    "            scores = self.classes[class_id][student_id]['scores']\n",
    "            avg = sum(scores) / len(scores)\n",
    "            max_score = max(scores)\n",
    "            min_score = min(scores)\n",
    "            return avg, max_score, min_score\n",
    "        else:\n",
    "            # 如果班级或学生不存在，则返回None\n",
    "            return None, None, None\n",
    "\n",
    "    def analyze_class_scores(self, class_id):\n",
    "        # 分析指定班级的所有学生成绩\n",
    "        if class_id in self.classes:\n",
    "            # 初始化班级成绩字典，用于存储每门课程的分数列表\n",
    "            class_scores = {i: [] for i in range(len(self.classes[class_id][list(self.classes[class_id].keys())[0]]['scores']))}\n",
    "            for student_id, student_info in self.classes[class_id].items():\n",
    "                # 遍历每个学生的成绩，并将成绩添加到对应课程的列表中\n",
    "                for i, score in enumerate(student_info['scores']):\n",
    "                    class_scores[i].append(score)\n",
    "            # 计算每门课程的平均分、最高分、最低分学生ID和最低分\n",
    "            class_avg = {i: sum(scores) / len(scores) for i, scores in class_scores.items()}\n",
    "            class_max = {i: max(scores) for i, scores in class_scores.items()}\n",
    "            class_min_student = {i: min(enumerate(scores), key=lambda x: x[1])[0] for i, scores in class_scores.items()}\n",
    "            class_min_score = {i: min(scores) for i, scores in class_scores.items()}\n",
    "            return class_avg, class_max, class_min_student, class_min_score\n",
    "        else:\n",
    "            return None, None, None, None\n",
    "\n",
    "    def modify_score(self, class_id, student_id, index, new_score):\n",
    "        # 修改指定学生的指定成绩\n",
    "        if class_id in self.classes and student_id in self.classes[class_id]:\n",
    "             # 如果班级和学生都存在，则修改成绩\n",
    "            self.classes[class_id][student_id]['scores'][index] = new_score\n",
    "            return True\n",
    "        else:\n",
    "            # 如果班级或学生不存在，则提示错误信息并返回False\n",
    "            print(f\"Class {class_id} or student {student_id} does not exist.\")\n",
    "            return False\n",
    "\n",
    "    def remove_student(self, class_id, student_id):\n",
    "        # 从指定班级中移除一个学生\n",
    "        if class_id in self.classes and student_id in self.classes[class_id]:\n",
    "            # 如果班级和学生都存在，则移除学生\n",
    "            del self.classes[class_id][student_id]\n",
    "            return True\n",
    "        else:\n",
    "            # 如果班级或学生不存在，则提示错误信息并返回False\n",
    "            print(f\"Class {class_id} or student {student_id} does not exist.\")\n",
    "            return False\n",
    "\n",
    "    def query_student_scores(self, class_id, student_id):\n",
    "        # 查询指定学生的成绩\n",
    "        if class_id in self.classes and student_id in self.classes[class_id]:\n",
    "            # 如果班级和学生都存在，则返回成绩列表\n",
    "            return self.classes[class_id][student_id]['scores']\n",
    "        else:\n",
    "            # 如果班级或学生不存在，则提示错误信息并返回None\n",
    "            print(f\"Class {class_id} or student {student_id} does not exist.\")\n",
    "            return None\n",
    "\n",
    "    def query_class_students(self, class_id):\n",
    "        # 查询指定班级的所有学生ID和姓名\n",
    "        if class_id in self.classes:\n",
    "            # 如果班级存在，则返回学生ID和姓名的字典\n",
    "            return {student_id: student_info['name'] for student_id, student_info in self.classes[class_id].items()}\n",
    "        else:\n",
    "            # 如果班级不存在，则提示错误信息并返回None\n",
    "            print(f\"Class {class_id} does not exist.\")\n",
    "            return None\n",
    "\n",
    "    def query_class_course_scores(self, class_id, course_index):\n",
    "        # 查询指定班级所有学生在指定课程索引的成绩\n",
    "        if class_id in self.classes:\n",
    "            # 如果班级存在，则遍历该班级的所有学生\n",
    "            return {student_id: student_info['scores'][course_index] for student_id, student_info in self.classes[class_id].items()}\n",
    "        else:\n",
    "            # 如果班级不存在，则打印错误信息并返回None\n",
    "            print(f\"Class {class_id} does not exist.\")\n",
    "            return None\n",
    "\n",
    "    def query_student_scores_by_name(self, class_id, name):\n",
    "        # 根据学生姓名查询指定班级中学生的成绩\n",
    "        if class_id in self.classes:\n",
    "            # 如果班级存在，则遍历该班级的所有学生\n",
    "            for student_id, student_info in self.classes[class_id].items():\n",
    "                # 检查学生姓名是否匹配\n",
    "                if student_info['name'] == name:\n",
    "                    # 如果匹配，则返回该学生的成绩列表\n",
    "                    return student_info['scores']\n",
    "        else:\n",
    "            # 如果班级不存在，则打印错误信息\n",
    "            print(f\"Class {class_id} does not exist.\")\n",
    "        return None\n",
    "\n",
    "# 示例用法\n",
    "analyzer = GradeAnalyzer()\n",
    "analyzer.add_class('1A')\n",
    "analyzer.add_student('1A', 'S001', 'Alice', [90, 85, 92])\n",
    "analyzer.add_student('1A', 'S002', 'Bob', [88, 95, 82])\n",
    "\n",
    "# 分析学生Alice的成绩\n",
    "avg, max_score, min_score = analyzer.analyze_student_scores('1A', 'S001')\n",
    "print(f\"Alice's average score: {avg:.2f}, Max score: {max_score}, Min score: {min_score}\")\n",
    "\n",
    "# 分析班级1A的成绩\n",
    "class_avg, class_max, class_min_score, class_min_student = analyzer.analyze_class_scores('1A')\n",
    "print(\"Class 1A scores analysis:\")\n",
    "for i, avg_score in class_avg.items():\n",
    "    print(f\"Course {i} - Average score: {avg_score:.2f}, Max score: {class_max[i]}, \"\n",
    "          f\"Min score student: {class_min_student[i]}, Min score: {class_min_score[i]}\")\n",
    "\n",
    "# 修改学生Bob的第二项成绩\n",
    "analyzer.modify_score('1A', 'S002', 1, 97)\n",
    "\n",
    "# 查询学生Alice的成绩\n",
    "print(f\"Alice's scores: {analyzer.query_student_scores('1A', 'S001')}\")\n",
    "\n",
    "# 查询班级1A的所有学生ID和姓名\n",
    "print(f\"Class 1A students: {analyzer.query_class_students('1A')}\")\n",
    "\n",
    "# 假设我们想要查询班级1A中所有学生在第一门课程（索引为0）的成绩\n",
    "print(f\"Class 1A students' scores for course 0: {analyzer.query_class_course_scores('1A', 0)}\")\n",
    "\n",
    "# 移除学生Alice\n",
    "analyzer.remove_student('1A', 'S001')\n",
    "\n",
    "# 再次查询班级1A的所有学生ID和姓名，确认Alice已被移除\n",
    "print(f\"Class 1A students after removing Alice: {analyzer.query_class_students('1A')}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
